package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author: liuaojun
 * @CreateTime: 2025-02-22
 * @Description:
 * @Version: 1.0
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo> implements RoomInfoService {

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;



    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        //查看房间基本信息
        RoomInfo roomInfo = super.getById(id);
        //查看公寓属性
        ApartmentInfo  apartmentInfo=apartmentInfoMapper.selectById(roomInfo.getApartmentId());

        //查看房间属性
        List<AttrValueVo> attrValueVoList= attrValueMapper.selectListByRoomId(id);


        //查看房间配套信息
        List<FacilityInfo> facilityInfoList= facilityInfoMapper.selectListByRoomId(id);
        //查看房间标签信息
        List<LabelInfo> LabelInfolist=labelInfoMapper.selectListByRoomId(id);

        //查看支付方式
        List<PaymentType> paymentTypeList=paymentTypeMapper.selectListByRoomId(id);
        //查看可选租期
        List<LeaseTerm> leaseTermList= leaseTermMapper.selectListByRoomId(id);

        //查看图片
        List<GraphVo> graphVoList =graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM,id);

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(LabelInfolist);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setGraphVoList(graphVoList);
        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
        //删除RoomInfo
        super.removeById(id);

        //删除GraphInfoList
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getId, id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

        //删除attrValueList
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

        //删除facilityInfoList
        LambdaQueryWrapper<RoomFacility> roomFacilityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityInfoLambdaQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(roomFacilityInfoLambdaQueryWrapper);

        //删除labelInfoList
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);

        //删除paymentTypeList
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

        //删除leaseTermList
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);

    }

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        boolean update = roomSubmitVo.getId() != null;
        super.saveOrUpdate(roomSubmitVo);
        if (update){
            //删除GraphInfoList
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getId, roomSubmitVo.getId());
            graphInfoService.remove(graphInfoLambdaQueryWrapper);

            //删除attrValueList
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

            //删除facilityInfoList
            LambdaQueryWrapper<RoomFacility> roomFacilityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityInfoLambdaQueryWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(roomFacilityInfoLambdaQueryWrapper);

            //删除labelInfoList
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(roomLabelLambdaQueryWrapper);

            //删除paymentTypeList
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

            //删除leaseTermList
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        }

        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfos = new ArrayList<>();//返回结果集合
            for (GraphVo graphVo : graphVoList){
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }

        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if(!CollectionUtils.isEmpty(attrValueIds)){
            ArrayList<RoomAttrValue> roomAttrValues = new ArrayList<>();
            for (Long attrValueId : attrValueIds){
                RoomAttrValue build = RoomAttrValue.builder().attrValueId(attrValueId).roomId(roomSubmitVo.getId()).build();
                roomAttrValues.add(build);
            }
            roomAttrValueService.saveBatch(roomAttrValues);
        }

        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds){
                RoomFacility build = RoomFacility.builder().facilityId(facilityInfoId).roomId(roomSubmitVo.getId()).build();
                roomFacilities.add(build);
            }
            roomFacilityService.saveBatch(roomFacilities);
        }

        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if(!CollectionUtils.isEmpty(labelInfoIds)){
            ArrayList<RoomLabel> roomLabels = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds){
                RoomLabel build = RoomLabel.builder().labelId(labelInfoId).roomId(roomSubmitVo.getId()).build();
                roomLabels.add(build);
            }
            roomLabelService.saveBatch(roomLabels);
        }

        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if(!CollectionUtils.isEmpty(paymentTypeIds)){
            ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds){
                RoomPaymentType build = RoomPaymentType.builder().paymentTypeId(paymentTypeId).roomId(roomSubmitVo.getId()).build();
                roomPaymentTypes.add(build);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if(!CollectionUtils.isEmpty(leaseTermIds)){
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds){
                RoomLeaseTerm build = RoomLeaseTerm.builder().leaseTermId(leaseTermId).roomId(roomSubmitVo.getId()).build();
                roomLeaseTerms.add(build);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
    }
}
